package com.pro.xwt.rest.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.pro.xwt.common.example.BMessageExample;
import com.pro.xwt.common.model.BEmployee;
import com.pro.xwt.common.model.BProject;
import com.pro.xwt.common.model.BTask;
import com.pro.xwt.common.service.BEmployeeService;
import com.pro.xwt.common.service.BMessageService;
import com.pro.xwt.common.service.BProjectService;
import com.pro.xwt.common.service.BTaskService;
import com.pro.xwt.rest.ResponseResult;

@Controller
@RequestMapping("/employees")
public class BEmployeeController {

	@Autowired
	BEmployeeService bEmployeeService;
	@Autowired
	BProjectService bProjectService;
	@Autowired
	BTaskService bTaskService;
	@Autowired
	BMessageService bMessageService;

	

	/**
	 * 修改资源
	 * 
	 * @param bEmployee
	 * @return
	 * 
	 * @see 暂无对用户进行修改操作
	 */
	@RequestMapping(method = { RequestMethod.PUT }, value = "")
	@ResponseBody
	public ResponseResult update(@RequestBody BEmployee bEmployee) {
		int flag=bEmployeeService.update(bEmployee);
		if(flag>0){
			return ResponseResult.success("修改资源成功", null);
		}
		return ResponseResult.fail("修改资源失败", null);
	}

	/**
	 * 
	 * 员工密码修改
	 * 
	 * @param bEmployee
	 * @return
	 * 
	 * @see 暂无对用户进行修改操作
	 */
	@RequestMapping(method = { RequestMethod.PATCH }, value = "/{id}")
	@ResponseBody
	public ResponseResult updateById(@PathVariable String id, @RequestBody Map<String, Object> map) {
		Integer flag = 0;
		List<BEmployee> list = null;
		if (id != null && map.get("oldPwd") != null) {
			if(map.get("newPwd").equals(map.get("reNewPwd"))){
				map.put("id", id);
				map.put("password", map.get("oldPwd"));
				list = bEmployeeService.queryListAll(map);
				if (list.size() > 0) {
					flag = bEmployeeService.updateById(map);
					if(flag>0){
						return ResponseResult.success("修改密码成功", flag);
					}else{
						return ResponseResult.fail("修改密码失败", flag);
					}
				} else {
					return ResponseResult.fail("原密码错误", flag);
				}
			}else{
				return ResponseResult.fail("新密码和确认密码不一致", flag);
			}
			
		}else{
			return ResponseResult.fail("原密码和新密码不能为空", flag);
		}
		
	}

	/**
	 * 
	 * 获取消息接口
	 * 
	 * @return
	 */
	@RequestMapping(method = { RequestMethod.GET }, value = "{id}/messages")
	@ResponseBody
	public ResponseResult getMessage(@PathVariable String id) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (id != null) {
			map.put("readState", "0");
			map.put("accpEmpId", id);
			map.put("deleteState", "0");
			PageHelper.orderBy("create_time desc");
			List<BMessageExample> lists = bMessageService.selectAll(map);
			return ResponseResult.success("获取消息成功", lists);
		}else{
			return ResponseResult.fail("传参错误", null);
		}
	}
	/**
	 * 获取单个资源
	 * 
	 * @param bEmployee
	 * @return
	 */
	@RequestMapping(method = { RequestMethod.GET }, value = "/{id}")
	@ResponseBody
	public ResponseResult get(@PathVariable String id, HttpSession session) {
		BEmployee be = bEmployeeService.findById(id);
		if (be == null) {
			return ResponseResult.fail("不存在该资源", null);
		}
		return ResponseResult.success("获取资源成功", be);
	}





	/**
	 * 获取某个员工下的项目
	 * 
	 * @param bEmployee
	 * @return
	 */
	@RequestMapping(method = { RequestMethod.GET }, value = "/{id}/projects")
	@ResponseBody
	public ResponseResult getProjectsByEmpId(@PathVariable String id, HttpServletRequest request) {
		BEmployee user = (BEmployee) request.getAttribute("user");
		PageHelper.startPage(0, 7);
		if (!user.getRole().equals("ADMIN")) {
			PageHelper.orderBy("p.create_time desc");
			List<BProject> list = bProjectService.selectProjectByEmployee(id);
			return ResponseResult.success("获取项目成功", list);
		} else {
			Map<String,Object> map= new HashMap<>();
			map.put("deleteState","0");
			PageHelper.orderBy("create_time desc");
			List<BProject> lists = bProjectService.queryListAll(map);
			return ResponseResult.success("获取项目成功", lists);
		}
	}

	/**
	 * 获取某个人任务接口
	 * 
	 * @param bEmployee
	 * @return
	 */
	@RequestMapping(method = { RequestMethod.GET }, value = "/{id}/tasks")
	@ResponseBody
	public ResponseResult getTasksByEmpId(@PathVariable String id) {
		List<BTask> list = bTaskService.selectTaskByEmployee(id);
		PageHelper.orderBy("create_time desc");
		return ResponseResult.success("获取资源成功", list);
	}

}
